<template>
  <el-container>
    <el-main>
      <el-row>
        <el-col :xs="0" :md="24">
          <el-card class="box-card">
            <template #header>
              <div class="card-header">
                <span>注册</span>
              </div>
            </template>
            <div>
              <el-form :model="regUser" status-icon :rules="rules" ref="regForm" label-width="80px">

                <el-form-item label="邮箱" prop="email">
                  <el-input v-model="regUser.email" autocomplete="off"></el-input>
                </el-form-item>

                <el-form-item label="昵称" prop="nickName">
                  <el-input v-model="regUser.nickName" autocomplete="off"></el-input>
                </el-form-item>

                <el-form-item label="密码" prop="pass">
                  <el-input type="password" v-model="regUser.pass" autocomplete="off"></el-input>
                </el-form-item>

                <el-form-item label="手机号" prop="phone">
                  <el-input v-model="regUser.phone" autocomplete="off"></el-input>
                </el-form-item>

                <el-form-item label="身份" prop="identity">
                  <el-select v-model="regUser.identity" placeholder="请选择">
                    <el-option label="学生" value="学生"></el-option>
                    <el-option label="咨询师" value="咨询师"></el-option>
                  </el-select>
                </el-form-item>

                <el-form-item label="介绍" v-show="regUser.identity === '咨询师'">
                  <el-input
                      type="textarea"
                      :rows="2"
                      placeholder="请输入内容"
                      v-model="regUser.summary">
                  </el-input>
                </el-form-item>

                <el-form-item>
                  <el-button type="primary" @click="submitForm('regForm')" style="float: left">注册</el-button>
                  <el-button @click="resetForm('regForm')" style="float: right">重置</el-button>
                </el-form-item>
              </el-form>
              <div class="card-footer">
                <router-link type="primary" style="float: right" to="/login">登录账号？</router-link>
              </div>
            </div>
          </el-card>
        </el-col>
      </el-row>

      <el-row>
        <el-col :xs="24" :sm="0">
          <el-card>
            <template #header>
              <div>
                <span>注册</span>
              </div>
            </template>
            <div>
              <el-form :model="regUser" status-icon :rules="rules" ref="regForm" label-width="80px">

                <el-form-item label="邮箱" prop="email">
                  <el-input v-model="regUser.email" autocomplete="off"></el-input>
                </el-form-item>

                <el-form-item label="昵称" prop="nickName">
                  <el-input v-model="regUser.nickName" autocomplete="off"></el-input>
                </el-form-item>

                <el-form-item label="密码" prop="pass">
                  <el-input type="password" v-model="regUser.pass" autocomplete="off"></el-input>
                </el-form-item>

                <el-form-item label="手机号" prop="phone">
                  <el-input v-model="regUser.phone" autocomplete="off"></el-input>
                </el-form-item>

                <el-form-item label="身份" prop="identity">
                  <el-select v-model="regUser.identity" placeholder="请选择">
                    <el-option label="学生" value="学生"></el-option>
                    <el-option label="咨询师" value="咨询师"></el-option>
                  </el-select>
                </el-form-item>

                <el-form-item label="介绍" v-show="regUser.identity === '咨询师'">
                  <el-input
                      type="textarea"
                      :rows="2"
                      placeholder="请输入内容"
                      v-model="regUser.summary">
                  </el-input>
                </el-form-item>

                <el-form-item>
                  <el-button type="primary" @click="submitForm('regForm')" style="float: left">注册</el-button>
                  <el-button @click="resetForm('regForm')" style="float: right">重置</el-button>
                </el-form-item>
              </el-form>
              <div class="card-footer">
                <el-link type="primary" style="float: right" @click="toPage('/login')">登录账号？</el-link>
              </div>
            </div>
          </el-card>
        </el-col>
      </el-row>
    </el-main>
  </el-container>
</template>

<script>
import {reactive, toRefs, getCurrentInstance} from "vue";
import {register} from "@/api/user";
import router from "@/router";

export default {
  name: "login",
  data(){
    return {
      regUser: {
        email: '',
        pass: '',
        identity: '学生',
        nickName: '',
        phone: '',
        summary: ''
      },
      rules: {
        email: [
          {required: true, message: '邮箱不能为空', trigger: 'blur'},
          {min: 5, max: 32, message: '邮箱长度在5-32个字符之间', trigger: 'blur'},
          {type: 'email', message: '邮箱格式错误', trigger: 'blur'},
        ],
        nickName: [
          {required: true, message: '昵称不能为空', trigger: 'blur'},
          {min: 2, max: 15, message: '昵称长度在2-15个字符之间', trigger: 'blur'}
        ],
        phone: [
          {
            validator: (rule, value, callback) => {
              if (value === '') {
                callback()
              } else if (!/^1(3[0-9]|4[01456879]|5[0-35-9]|6[2567]|7[0-8]|8[0-9]|9[0-35-9])\d{8}$/.test(value)) {
                callback("手机号格式不正确");
              } else {
                callback()
              }
            }, trigger: 'blur'
          }
        ],
        pass: [
          {required: true, message: '密码不能为空', trigger: 'blur'},
          {min: 5, max: 20, message: '密码长度在5-20个字符之间', trigger: 'blur'}
        ],
        identity: [
          {required: true, message: '请选择身份', trigger: 'blur'}
        ]
      }
    }
  },
  methods: {
    submitForm(formName){
      this.$refs[formName].validate((valid) => {
        if (valid) {
          register(this.regUser).then(res => {
            if (res.status) {
              router.push('/login')
            }
          })
        }
      })
    },
    resetForm(formName){
      this.$refs[formName].resetFields();
    },
    toPage(path){
      router.push(path)
    }
  }
}
</script>

<style lang="less" scoped>
.el-container {
  width: 100%;
  height: 100%;
  background-color: #2c3e50;

  .el-main {
    height: 600px;
    margin: auto;

    .box-card {
      width: 400px;
      margin: auto;

      .card-header {
        text-align: left;
      }
    }
  }
}
</style>
